var XMLHttpReq;

function logout(){
    var logedusername = document.getElementById("logedusername");
    createXMLHttpRequest();	
	var url = "LogoutServlet?logedusername=" + logedusername.value;
	XMLHttpReq.onreadystatechange = loginoutListen;
	XMLHttpReq.open("GET", url, true);
	XMLHttpReq.send(null);
     
}
function loginoutListen(){
   if (XMLHttpReq.readyState == 4) {
		if (XMLHttpReq.status == 200) {
				var msg = XMLHttpReq.responseText;
				if(msg==1){
				    window.setTimeout("window.location.reload(true)", 100);
				} 
		} 
	}
}

function openErrorLogin(){
	showWindow("loginWindow");
	error = document.getElementById("loginMsg");
	error.innerHTML = "<font color='red'>您的权限不足!请登陆后尝试</font>";
	error.style.display = "block";
	document.login.username.focus();
}

function openErrorMsgLogin(errormsg){
	showWindow("loginWindow");
	error = document.getElementById("loginMsg");
	error.innerHTML = "<font color='red'>"+errormsg+"</font>";
	error.style.display = "block";
	document.login.username.focus();
}

function createXMLHttpRequest() {
	if (window.XMLHttpRequest) {
		XMLHttpReq = new XMLHttpRequest();
	} else {
		if (window.ActiveXObject) {
			try {
				XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
				}
			}
		}
	}
}

function isAutoLogin(){

	var auto=document.login.autologin.value;
	if(auto=="true"){
		document.login.autologin.value="false";
	}
	if(auto=="false"){
		alert('2个星期内将不用登陆，如果您在功能场合请不要使用此功能!');
		document.login.autologin.value="true";
	}
}

function openLogin(){
	showWindow("loginWindow");
	document.login.username.focus();
}

/*
 * 
 * 登陆验证checkLogin()
 * 
 */

function checkLogin(){
	var username = document.login.username.value;
	var password = document.login.password.value;
	var time=Math.random();
	if(username==""){
		error = document.getElementById("loginMsg");
		error.innerHTML = "<font color='red'>用户名不能为空!</font>";
		error.style.display = "block";
		return false;
	}
	if(password==""){
		error = document.getElementById("loginMsg");
		error.innerHTML = "<font color='red'>密码不能为空!</font>";
		error.style.display = "block";	
		return false;
	}
	return true;
}


/*
 * 
 * 登陆请求loginStart()
 * 
 */

function loginStart() {
	if(checkLogin()==true){
		error = document.getElementById("loginMsg");
		
		var type=document.login.type.value;
		var username = document.login.username.value;
		var password = document.login.password.value;
		var autologin = document.login.autologin.value;
		
		if(type=="member"){
		   error.innerHTML = "<img src='images/loading.gif' align='absmiddle' />正在登陆...";
		}else if(type=="admin"){
		   error.innerHTML = "<img src='../images/loading.gif' align='absmiddle' />正在登陆...";
		}
		error.style.display = "block";		
		
		var time=Math.random();		
		createXMLHttpRequest();
		var url;
		if(type=="member"){
		       url = "LoginServlet?username=" + username + "&password=" + password + "&type="+type+ "&autologin="+autologin;
		}else if(type=="admin"){
		       url = "../LoginServlet?username=" + username + "&password=" + password + "&type="+type;
		}
		
		XMLHttpReq.onreadystatechange = loginListen;
		XMLHttpReq.open("GET", url, true);
		XMLHttpReq.send(null);
	}
}

/*
 * 
 * 登陆监听loginListen()
 * 
 */

function loginListen() {
	if (XMLHttpReq.readyState == 4) {
		if (XMLHttpReq.status == 200) {
			loginProcess();
		} else {
			error = document.getElementById("loginMsg");
			error.innerHTML = "<font color='red'>服务器无响应!请重新尝试.</font>";
			error.style.display = "block";
		}
	}
}

/*
 * 
 * 登陆处理loginProcess()
 * 
 */

function loginProcess() {

	var msg = XMLHttpReq.responseText;
	
	if (msg!=0) {
		error = document.getElementById("loginMsg");
		error.innerHTML = "欢迎<font color='red' >"+document.login.username.value+"</font>!3秒后返回...";
		error.style.display = "block";
		window.setTimeout("window.location.reload(true)", 2000);
	} else {
		error = document.getElementById("loginMsg");
		error.innerHTML = "<font color='red'>帐户密码错误或权限不足...</font>";
		error.style.display = "block";
	}
}

/*
 * 
 * 关闭登陆框closeLogin()
 * 
 */

function closeLogin(){

	document.login.password.value="";
	error = document.getElementById("loginMsg");
	error.style.display = "none";
	hideWindow("loginWindow");	
}


/*
 * 
 * 打开注册框openRegister()
 * 
 */

function openRegister(){
    hideWindow("loginWindow");
	showWindow("registerWindow");
	document.register.username.focus();
}

/*
 * 
 * 验证注册合法性checkRegister()
 * 
 */
 
 //qu chu kong ge
function isTrim(string) {
  if((string.replace(/\s/g,"").length<string.length)||(string.replace(/\s/g,"").length<1)){
     return false;
  }
    return true;
 } 
 
function trim(string){
   return string.replace(/\s/g,"");
 }
 
function isEmail(email){
     var emailtest=/^(.+)@(.+)$/;
     if(emailtest.test(email)){
	   return true;
     }else{
	   return false;
     }
}

function isUsername(username)
{
     var usernamereg=/^(\w|\_|[\u4E00-\u9FA5])*$/;

     if(usernamereg.test(username))
     {
	   return true;
     }
     else
     {
	   return false;
     }
}

function isPassword(password){
 
   var regu = "^[0-9a-zA-Z]+$"; 
   var re = new RegExp(regu); 
   if (re.test(password)) { 
   return true; 
   }else{ 
    return false; 
   } 
} 


function checkRegister(){
	var username = document.register.username.value;
	var password = document.register.password.value;
	var repassword = document.register.repassword.value;
	var email = document.register.email.value;
	
	    
	error = document.getElementById("registerMsg");
	
	if(!isUsername(username)){
		error.innerHTML = "<font color='red'>会员名不能为空!</font>";
		document.register.username.focus();
		error.style.display = "block";
		return false;
	}
	if(username.length<4||username.length>18){  
		error.innerHTML = "<font color='red'>会员名长度在4-18字符之间!</font>";
		error.style.display = "block";
		return false;
	}
	if(!isPassword(password)){	
		error.innerHTML = "<font color='red'>密码不能为空!</font>";
		error.style.display = "block";	
		return false;
	}
	
	if(password.length>20){	
		error.innerHTML = "<font color='red'>密码长度小于20个字符!</font>";
		error.style.display = "block";	
		return false;
	}
	if(!isPassword(repassword)){	
		error.innerHTML = "<font color='red'>确认密码不能为空!</font>";
		error.style.display = "block";	
		return false;
	}
	
	if(repassword.length>20){	
		error.innerHTML = "<font color='red'>确认密码长度小于20个字符!</font>";
		error.style.display = "block";	
		return false;
	}
	if(password!=repassword){
		error.innerHTML = "<font color='red'>两次密码不一致!</font>";
		error.style.display = "block";	
		return false;
	}
	
	if(!isEmail(email)){
		error.innerHTML = "<font color='red'>Email地址有误!</font>";
		error.style.display = "block";	
		return false;
	}
	
	var qq = document.register.qq.value;
	var msn = document.register.msn.value;
	var site = document.register.site.value;
	var blog = document.register.blog.value;
	
	if(trim(qq)!=""&&!isQQ(qq)){
	   error.innerHTML = "<font color='red'>QQ格式有误!</font>";
	   error.style.display = "block";	
	   return false;
	}
	if(trim(msn)!=""&&!isEmail(msn)){
	   error.innerHTML = "<font color='red'>MSN格式有误!</font>";
	   error.style.display = "block";	
	   return false;
	}
	if(trim(site)!=""&&!isSite(site)){
	  error.innerHTML = "<font color='red'>个人主页格式有误!</font>";
	  error.style.display = "block";	
	  return false;
	}
	if(trim(blog)!=""&&!isSite(blog)){
	  error.innerHTML = "<font color='red'>Blog格式有误!</font>";
	  error.style.display = "block";	
	  return false;
	}
	return true;	
}

function registerStart() {

	if(checkRegister()==true){
	
		error = document.getElementById("registerMsg");
		error.innerHTML = "<img src='images/loading.gif' align='absmiddle' />正在注册...";
		error.style.display = "block";	
		
		var username = document.register.username.value;
		var password = document.register.password.value;
		var email = document.register.email.value;
	
		var birthday=document.register.birthday.value;
		var location = document.register.location.value;
		var qq = document.register.qq.value;
		var msn = document.register.msn.value;
		var site = document.register.site.value;
		var blog = document.register.blog.value;
		var time=Math.random();	
		
		createXMLHttpRequest();

		var url = "RegisterServlet?username=" + username + "&password=" + password + "&email=" + email+"&birthday="+birthday+"&qq="+qq+"&msn="+msn+"&site="+site+"&location="+location+"&blog="+blog;
		XMLHttpReq.onreadystatechange = registerListen;
		XMLHttpReq.open("GET", url, true);
		XMLHttpReq.send(null);
	}
}

function registerListen() {
	if (XMLHttpReq.readyState == 4) {
		if (XMLHttpReq.status == 200) {
			registerProcess();
		} else {
			error = document.getElementById("registerMsg");
			error.innerHTML = "服务器无响应!";
			error.style.display = "block";
		}
	}
}

function registerProcess() {

	var msg = XMLHttpReq.responseText;
	
	if (msg == 1) {
		error = document.getElementById("registerMsg");
		var username = document.register.username.value;
		error.innerHTML = "欢迎:<font color='red'>"+username+"</font>注册.3秒钟后返回";
		error.style.display = "block";
		window.setTimeout("window.location.reload(true)", 2000);
	} else if(msg == 0){
		error = document.getElementById("registerMsg");
		error.innerHTML = "用户已存在";
		error.style.display = "block";
	} else if(msg==-1){
	    error = document.getElementById("registerMsg");
		error.innerHTML = "注册出错,请稍后重试...";
		error.style.display = "block";
	}
}

function closeRegister(){
	document.register.username.value="";
	document.register.password.value="";
	document.register.repassword.value="";
	document.register.email.value=""
	error = document.getElementById("registerMsg");
	error.style.display = "none";
	hideWindow("registerWindow");	
}



function openNewTopic(){
	showWindow("newtopicWindow");
	document.newtopic.title.focus();
}

function checkNewTopic(){
	var title=document.newtopic.topictitle.value;
	var oEditor = FCKeditorAPI.GetInstance('topiccontent');
	var oDOM = oEditor.EditorDocument;
	var content = oDOM.body.innerText;
	if(trim(title).length<1){
		error = document.getElementById("newtopicMsg");
		error.innerHTML = "<font color='red'>对不起，主题名称不能为空!</font>";
		error.style.display = "block";
		return false;
	}
	if(trim(content).length<1){
		error = document.getElementById("newtopicMsg");
		error.innerHTML = "<font color='red'>对不起，主题内容不能为空!</font>";
		error.style.display = "block";	
		return false;
	}
	error = document.getElementById("newtopicMsg");
	error.innerHTML = "正在发表主题...请稍等.";
	error.style.display = "block";	
	document.newtopic.submit();
}


function closeNewTopic(){
	FCKeditorAPI.GetInstance("topiccontent").SetHTML("");
	document.newtopic.title.value="";
	error = document.getElementById("newtopicMsg");
	error.style.display = "none";
	hideWindow("newtopicWindow");	
}


function openNewPost(){
	showWindow("newpostWindow");
}

function checkNewPost(){
	var posttitle=document.newpost.posttitle.value;
	var oEditor = FCKeditorAPI.GetInstance('postcontent');
	var oDOM = oEditor.EditorDocument;
	var postcontent = oDOM.body.innerText;
	if(trim(postcontent)<1){
		error = document.getElementById("newpostMsg");
		error.innerHTML = "<font color='red'>对不起，回复内容不能为空!</font>";
		error.style.display = "block";	
		return false;
	}
	error = document.getElementById("newpostMsg");
	error.innerHTML = "正在发表回复...请稍等.";
	error.style.display = "block";	
	document.newpost.submit();	
}


function closeNewPost(){
	FCKeditorAPI.GetInstance("postcontent").SetHTML("");
	document.newpost.posttitle.value="";
	error = document.getElementById("newpostMsg");
	error.style.display = "none";
	hideWindow("newpostWindow");	
}

function showMessage(msg){
	if(msg!=""){
		showWindow("message");
		setTimeout("hideWindow('message')", 1000);
	}
}

<!--校验邮件与密码-->
function checkMailPassword() {
	var pass = 1;
	var mail = document.frmMain.email.value;
	var formerpassword = document.frmMain.formerpassword.value;
	var password = document.frmMain.password.value;
	var repassword = document.frmMain.repassword.value;
	
	var warnmsg = document.getElementById("warnmsg");
	var servermsg = document.getElementById("servermsg");
	servermsg.innerHTML="";
	
	if (!isEmail(trim(mail))) {
		warnmsg.innerHTML = "\u90ae\u4ef6\u683c\u5f0f\u4e0d\u6b63\u786e...";
		pass = 0;
	}
	
	if (formerpassword != "") {
		if (password == "" && repassword == "") {
			warnmsg.innerHTML = "\u8bf7\u8f93\u5165\u65b0\u5bc6\u7801...";
			pass = 0;
		} else {
			if (password != repassword) {
				warnmsg.innerHTML = "\u4e24\u6b21\u5bc6\u7801\u8f93\u5165\u4e0d\u4e00\u81f4...";
				pass = 0;
			}else if(!isPassword(password)||password.length<4){
			   	warnmsg.innerHTML = "密码格式不正确或长度小于4...";
		        pass = 0;
			}
		}
	}else if(formerpassword == ""){
	   if(password!=""||repassword!=""){
	      	warnmsg.innerHTML = "请输入原密码...";
		    pass = 0;
	   }
	}
	
	if(pass==1){
	   document.frmMain.submit();
	}
}

<!--校验联系信息-->
function checkContactInfo(){
    var pass = 1;
    
    var site = document.frmMain.site.value;
    var blog = document.frmMain.blog.value;
    var msn = document.frmMain.msn.value;
    var qq = document.frmMain.qq.value;
    var home = document.frmMain.home.value;
    
    var warnmsg = document.getElementById("warnmsg");
	var servermsg = document.getElementById("servermsg");
	
	servermsg.innerHTML="";	
	if(site!=""&&!isSite(trim(site))){
	   	warnmsg.innerHTML = "个人主页格式不正确...";
		pass = 0;
	}
	if(blog!=""&&!isSite(trim(blog))){
		warnmsg.innerHTML = "Blog格式不正确...";
		pass = 0;
	}
	if(msn!=""&&!isEmail(trim(msn))){
	   warnmsg.innerHTML = "MSN格式不正确...";
	   pass = 0;
	}
	if(qq!=""&&!isQQ(trim(qq))){
	   warnmsg.innerHTML = "QQ格式不正确...";
	   pass = 0;
	}
	if(pass==1){
	    document.frmMain.submit();
	}
}

function isSite(site){
     var sitetest=/^[a-zA-z]+:\/\/(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$/;
     if(sitetest.test(site)){
	   return true;
     }else{
	   return false;
     }
}

function isQQ(qq){
     var qqtest=/^[1-9]*[1-9][0-9]*$/;
     if(qqtest.test(qq)){
	   return true;
     }else{
	   return false;
     }
}